package com.limh.jkzs.util

import android.content.Context
import android.content.SharedPreferences
import android.support.v4.content.SharedPreferencesCompat

/**
 * @title： ${name}
 * @package： com.limh.jkzs.util
 * @description：
 * @author： limh
 * @date： 2018/1/24
 * Copyright @2017 Corpration Name
 */
object CacheUtils {
    val FILE_NAME: String = "JKZK"

    fun put(context: Context, key: String, any: Any) {
        val sp: SharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
        val editer: SharedPreferences.Editor = sp.edit()

        if (any is String)
            editer.putString(key, any)
        else if (any is Int)
            editer.putInt(key, any)
        else if (any is Float)
            editer.putFloat(key, any)
        else if (any is Long)
            editer.putLong(key, any)
        else if (any is Boolean)
            editer.putBoolean(key, any)
        SharedPreferencesCompat.EditorCompat.getInstance().apply(editer)
    }

    fun get(context: Context, key: String, default: Any?): Any {
        val sp: SharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)

        return when (default) {
            is String -> sp.getString(key, default)
            is Int -> sp.getInt(key, default)
            is Float -> sp.getFloat(key, default)
            is Long -> sp.getLong(key, default)
            is Boolean -> sp.getBoolean(key, default)
            else -> ""
        }
    }
}